package org.mseco.doc.expensetrack.util;

import java.util.Calendar;

public class CalendarUtil {

	public static Calendar getStartOfWeek(Calendar calendar){
		Calendar weekStart = (Calendar)calendar.clone();
		weekStart.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
		timeToZero(weekStart);
		return weekStart;
	}
	
	public static Calendar get7DaysInThePast(Calendar calendar){
		Calendar weekStart = (Calendar)calendar.clone();
		weekStart.roll(Calendar.DATE, -7);
		timeToZero(weekStart);
		return weekStart;
	}
	
	public static Calendar[] getPreviousMonth(Calendar calendar){
		Calendar[] calendars = new Calendar[2];
		Calendar weekStart = (Calendar)calendar.clone();		
		weekStart.roll(Calendar.MONTH, -1);
		weekStart.set(Calendar.DATE, 1);
		calendars[0] = weekStart;
		timeToZero(weekStart);
		
		weekStart = (Calendar)calendar.clone();
		weekStart.roll(Calendar.MONTH, -1);
		weekStart.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
		calendars[1] = weekStart;
				
		return calendars;
	}
	
	public static Calendar get30DaysInThePast(Calendar calendar){
		Calendar weekStart = (Calendar)calendar.clone();
		weekStart.add(Calendar.DATE, -30);
		timeToZero(weekStart);
		return weekStart;
	}
	
	public static Calendar getStartOfMonth(Calendar calendar){
		Calendar weekStart = (Calendar)calendar.clone();
		weekStart.set(Calendar.DAY_OF_MONTH,1);
		timeToZero(weekStart);
		return weekStart;
	}
		
	public static void timeToZero(Calendar calendar){
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.MILLISECOND, 0);
	}
	
	public static void timeToMax(Calendar calendar){
		calendar.set(Calendar.HOUR, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.MILLISECOND, 59);
	}
}
